/**
 * @ResCacheMgr.as
 *
 * @author sodaChen mail:sujun10@qq.com
 * @version 1.0
 * <br>Program Name:ASFrame
 * <br>Date:2016-8-25
 */
package com.asframe.mgr
{
	import com.asframe.Const;
	import com.asframe.collections.maps.HashMap;
	import com.asframe.pattern.Singleton;
	import com.asframe.utils.FunctionUtils;

	/**
	 * 资源缓存管理机制
	 * @author sodaChen
	 * #Date:2016-8-25
	 */
	public class ResCacheMgr extends Singleton
	{
		private static var instance:ResCacheMgr;

		/** 长期存在 **/
		private var alangMap:HashMap;
		/** 场景存在 **/
		private var sceneMap:HashMap;
		/**
		 * 获取实例的静态方法实例
		 * @return
		 *
		 */
		public static function getInstance():ResCacheMgr
		{
			if(instance == null)
			{
				instance = new ResCacheMgr();
			}
			return instance;
		}

		public function ResCacheMgr()
		{
			sceneMap = new HashMap();
			alangMap = new HashMap();
		}
		/**
		 * 获取资源
		 * @param key
		 * @return
		 *
		 */
		public function getRes(key:*):*
		{
			if(sceneMap.hasKey(key))
				return sceneMap.get(key);
			return alangMap.get(key);
		}
		/**
		 * 存放资源
		 * @param key 唯一key
		 * @param res 资源
		 * @param cacheType 缓存类型
		 *
		 */
		public function putRes(key:*,res:*,cacheType:int = Const.SCENE_CACHE):void
		{
			if(cacheType == Const.SCENE_CACHE)
				sceneMap.put(key,res);
			else
				alangMap.put(key,res);
		}
		/**
		 * 删除资源
		 * @param key
		 * @return 返回缓存的资源
		 *
		 */
		public function removeRes(key:*):*
		{
			if(sceneMap.hasKey(key))
				return sceneMap.remove(key);
			return alangMap.remove(key);
		}
		/**
		 * 改变资源的缓存类型
		 * @param key key
		 * @param cacheType 缓存类型
		 *
		 */
		public function changeCacheType(key:*,cacheType:int = Const.SCENE_CACHE):void
		{
			var res:* = getRes(key);
			if(res != null)
				putRes(key,res,cacheType);
		}
		/**
		 * 清除缓存
		 * @param cacheType 默认是清除场景缓存，否则全体缓存都清除
		 *
		 */
		public function clearCache(cacheType:int = Const.SCENE_CACHE):void
		{
			//清除还得优化
			if(cacheType == Const.SCENE_CACHE)
				sceneMap.clear();
			else
				alangMap.clear();
		}
		/**
		 * 加载并且缓存资源
		 * @param url 加载路径
		 * @param complete 成功回调函数
		 * @param target 附加参数
		 * @param cacheType
		 *
		 */
		public function loadRes(key:*,url:String,complete:Function,target:* = null,cacheType:int = Const.SCENE_CACHE):void
		{
			if(key == null)
				key = url;
			var resData:CacheResData = new CacheResData(key,complete,target,cacheType);
			ResMgr.getInstance().load(url,complete,resData);
		}
		private function onLoadRes(res:*,resData:CacheResData):void
		{
			//进行存放
			putRes(resData.key,res,resData.cacheType);
			FunctionUtils.callFuncTarget(res,resData.complete,resData.target);
		}
	}
}
class CacheResData
{
	public var key:*;
//	public var url:String;
	public var complete:Function;
	public var target:*;
	public var cacheType:int;
//	public function CacheResData(key:*,url:String,complete:Function,target:*,cacheType:int)
	public function CacheResData(key:*,complete:Function,target:*,cacheType:int)
	{
		this.key = key;
		this.complete = complete;
		this.target = target;
		this.cacheType = cacheType;
	}
}